<?php

class Menus extends MX_Controller {

    function __construct() {
        parent::__construct();
    }

    function get_main_menus($options = array()) 
    {
        $menus = get_cache('menus');
        $options['menus'] = $menus;

        $output1 = array();
        $output2 = array();
        foreach ($menus as $menu) {
            $output1[$menu['id']] = $menu['url_path'];
            $output2[$menu['url_path']] = $menu['parent_id'];
        }

//        $options['menus']       = $this->menus_model->get_menus($options);
        $options['parent_url'] = (isset ($options['current_menu']) && isset ($output2[$options['current_menu']]) && isset($output1[$output2[$options['current_menu']]])) ? $output1[$output2[$options['current_menu']]] : '';
        if ($options['parent_url'] != '' && $output2[$options['parent_url']] != 0)
            $options['grandparent_url'] = isset($output1[$output2[$options['parent_url']]]) ? $output1[$output2[$options['parent_url']]] : '';

        $options['parent_id'] = FRONT_END_MENU;
        $top_menus = '<ul class="m_menu">';
        $top_menus .= $this->_visit_main_menus($options);
        $top_menus .= '</ul>';
        $top_menus = str_replace('<ul></ul><span class="submenu-top"></span>', '', $top_menus);
        return $top_menus;
    }
    
    private function _visit_main_menus($params = array()) 
    {
        $output = '';
        $sub_menus = $this->_get_sub_menus($params);
        
        foreach ($sub_menus as $menu) 
        {
            if($menu['css'] != 3)
            {
                $params['parent_id'] = $menu['id'];
                $style = ($menu['url_path'] === $params['current_menu']) ? 'active' : '';
                $style1 = ($menu['url_path'] === $params['parent_url']) ? 'active ' : '';
                $style2 = ($menu['url_path'] === $params['grandparent_url']) ? 'active ' : '';

                $output .= '<li class="' . $style . $style1 . $style2 . '"><a href="' . $menu['url_path'] . '">' . $menu['caption'] . '</a>';
                $output .= '<ul>';
                $output .= $this->_visit_main_menus($params);
                $output .= '</ul><span class="submenu-top"></span></li>';
            }
            
        }
        return $output;
    }

    function get_mobile_main_menus($options = array()) {
        $menus = get_cache('menus');
        $options['menus'] = $menus;

        $output1 = array();
        $output2 = array();
        foreach ($menus as $menu) {
            $output1[$menu['id']] = $menu['url_path'];
            $output2[$menu['url_path']] = $menu['parent_id'];
        }

        $options['parent_url'] = isset($output1[$output2[$options['current_menu']]]) ? $output1[$output2[$options['current_menu']]] : '';
        if ($output2[$options['parent_url']] != 0)
            $options['grandparent_url'] = isset($output1[$output2[$options['parent_url']]]) ? $output1[$output2[$options['parent_url']]] : '';

        $options['parent_id'] = FRONT_END_MENU;
        $top_menus = '<ul id="header-menu" class="menu">';
        $top_menus .= $this->_visit_mobile_main_menus($options);
        $top_menus .= '</ul>';
//        $top_menus = str_replace('<ul class="sub_menu"></ul>', '', $top_menus);
        return $top_menus;
    }

    private function _visit_mobile_main_menus($params = array()) {
        $output = '';
        $sub_menus = $this->_get_sub_menus($params);
        $i = 0;
        foreach ($sub_menus as $menu) {
            $output .= '<li><a href="' . $menu['url_path'] . '">' . $menu['caption'] . '</a>';
            $params['parent_id'] = $menu['id'];
            $sub_child = $this->_get_sub_menus($params);
            if (count($sub_child) > 0) {
                $output .= '<ul class="sub-menu">';
                $output .= $this->_visit_mobile_main_menus($params);
                $output .= '</ul></li>';
            } else {
                $output .= '</li>';
            }
        }
        return $output;
    }

    private function _get_sub_menus($params = array()) {
        $menus = $params['menus'];
        $sub_menus = array();
        foreach ($menus as $index => $menu) {
            if ($menu['parent_id'] == $params['parent_id'])
                $sub_menus[$index] = $menu;
        }
        return $sub_menus;
    }

    private function _get_sub_menus_admin($params = array()) {
        $menus = $params['menus'];
        $sub_menus = array();

        foreach ($menus as $index => $menu) {
            if ($menu->parent_id == $params['parent_id'])
                $sub_menus[$index] = $menu;
        }
        return $sub_menus;
    }

    function get_dashboard_menus($options = array()) {
        $options['css'] = 'menu';
        $role = $this->phpsession->get('roles_id');

        $role_param = array();
        if ($role != ROLE_ADMINISTRATOR && $role != ROLE_MANAGER)
            $role_param = array('roles_id' => $role);

        $options['menus'] = $this->menus_model->get_menus($role_param);


        $options['parent_id'] = BACK_END_MENU;
        $top_menus = '<ul id="nav">';
        $top_menus .= $this->_visit_admin_menus($options);
        $top_menus .= '</ul>';
        $view_data = array();
        $view_data['top_menus'] = $top_menus;
        return $this->load->view('menus/dashboard_menus', $view_data, TRUE);
    }

    /**
     * Đổi lại CSS sang thẻ A
     *
     * @param type $params
     * @return string
     */
    private function _visit_admin_menus($params = array()) {
        $output = '';
        $sub_menus = $this->_get_sub_menus_admin($params);
        foreach ($sub_menus as $menu) {

            $params['parent_id'] = $menu->id;
            $output .= '<li><a href="' . $menu->url_path . '">' . $menu->caption . '</a>';
            $output .= '<ul>';
            $output .= $this->_visit_admin_menus($params);
            $output .= '</ul></li>';
        }
        return $output;
    }

    function get_bottom_menus($options = array()) {
        $options['parent_id'] = FRONT_END_MENU;
//        $menus = get_cache('menus');
//        $options['menus']           = $menus;
        $options['menus'] = $this->menus_model->get_menus($options + array('array' => TRUE, 'lang' => $this->_lang));
        $options['bottom_menus'] = TRUE;
        $bottom_menus = '<ul class="f_menu">';
        $bottom_menus .= $this->_visit_main_menus($options);
        $bottom_menus .= '</ul>';
        return $bottom_menus;
    }

    function get_menu_data($options = array()) {
        $options['lang'] = switch_language($this->uri->segment(1));
        $menu = $this->menus_model->get_menus($options);
        return $menu;
    }

    function get_left_menus($options = array())
    {
        $menus = get_cache('menus');
        $options['menus'] = $menus;

        $output1 = array();
        $output2 = array();
        foreach ($menus as $menu) {
            $output1[$menu['id']] = $menu['url_path'];
            $output2[$menu['url_path']] = $menu['parent_id'];
        }

//        $options['menus']       = $this->menus_model->get_menus($options);
        $options['parent_url'] = (isset ($options['current_menu']) && isset ($output2[$options['current_menu']]) && isset($output1[$output2[$options['current_menu']]])) ? $output1[$output2[$options['current_menu']]] : '';
        if ($options['parent_url'] != '' && $output2[$options['parent_url']] != 0)
            $options['grandparent_url'] = isset($output1[$output2[$options['parent_url']]]) ? $output1[$output2[$options['parent_url']]] : '';

        $options['parent_id'] = FRONT_END_MENU;
        $left_menus                 = '';
        $left_menus                 .= $this->_visit_left_menus($options);
        $left_menus                 = str_replace('<ul class="subs"></ul>', '', $left_menus);
        $view_data['left_menus']    = $left_menus;
        $view_data['title']         = isset($output2[$options['parent_id']]) ? $output2[$options['parent_id']] : __('IP_main_services');
        return $this->load->view('menus/left_menus', $view_data, TRUE);
    }
    
    private function _visit_left_menus($params = array())
    {
        $output                 = '';
        $sub_menus              = $this->_get_sub_menus($params);
        foreach($sub_menus as $menu)
        {
            $params['parent_id']    = $menu['id'];
            if($menu['url_path']===$params['current_menu'])
                $style                  = ' class = " menu_s2 select"';
            else
                $style                  = ' class = " menu_s2"';
            $output                 .= '<li' . $style . '><a href="' . $menu['url_path'] . '"><img src="/images/icon5.gif">' . $menu['caption'] . '</a>';
            $output                 .= '<ul class="subs">';
            $output                 .= $this->_visit_left_menus($params);
            $output                 .= '</ul></li>';
        }
        return $output;
    }

}

?>